package com.yuanmei.views;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yuanmei.model.PriceStat;

import java.util.List;
import java.util.Locale;

public class PriceStatAdapter extends RecyclerView.Adapter<PriceStatAdapter.TickDetailViewHolder> {
    private List<PriceStat> mPriceStatList;
    private ItemClickListener mClickListener;

    // 点击事件接口
    public interface ItemClickListener {
        void onItemClick(RecyclerView.Adapter adapter, View view, int position);
    }

    PriceStatAdapter(List<PriceStat> statList) {
        this.mPriceStatList = statList;
    }

    void setPriceStatList(List<PriceStat> statList) {
        this.mPriceStatList = statList;
        notifyDataSetChanged();
    }

    private int mCurrentClickPosition = -1;
    void setCurrentClickPosition(int position) {
        mCurrentClickPosition = position;
    }

    int getCurrentClickPosition() {
        return mCurrentClickPosition;
    }

    @NonNull
    @Override
    public TickDetailViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.price_stat_item,
                parent, false);
        return new TickDetailViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final TickDetailViewHolder holder, final int position) {
        if (mPriceStatList != null) {
            PriceStat stat = mPriceStatList.get(position);
            holder.mTvPrice.setText(String.format(Locale.CHINA,
                    "%.2f ", stat.getPrice()));
            holder.mTvPercent.setText(String.format(Locale.CHINA,
                    "%.2f%%", stat.getAmountPct()));
            holder.mTvAmount.setText(String.format(Locale.CHINA,
                    "%d笔 %.2f万",  stat.getCount(), stat.getAmount() / 10000.0f));
            if (mCurrentClickPosition == position) {
                holder.mItemView.setBackgroundColor(Color.LTGRAY);
            } else {
                holder.mItemView.setBackgroundColor(Color.TRANSPARENT);
            }
            int textColor;
            if (stat.getPropertyAmount() > 0) {
                // red
                textColor = Color.rgb(237,19,18);
            } else {
                // green
                textColor = Color.rgb(46,159,72);
            }
            holder.mTvPrice.setTextColor(textColor);
            holder.mTvPercent.setTextColor(textColor);
            holder.mTvAmount.setTextColor(textColor);

            holder.mItemView.setOnClickListener(v -> {
                if (mClickListener != null) {
                    mClickListener.onItemClick(PriceStatAdapter.this, v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mPriceStatList == null || mPriceStatList.size() < 1) {
            return 0;
        }
        return mPriceStatList.size();
    }

    void setClickListener(PriceStatAdapter.ItemClickListener clickListener) {
        mClickListener = clickListener;
    }

    class TickDetailViewHolder extends RecyclerView.ViewHolder {
        private View mItemView;
        private TextView mTvPrice;
        private TextView mTvPercent;
        private TextView mTvAmount;

        TickDetailViewHolder(View itemView) {
            super(itemView);
            mItemView = itemView;
            mTvPrice = mItemView.findViewById(R.id.stat_price);
            mTvPercent = mItemView.findViewById(R.id.stat_amount_percent);
            mTvAmount = mItemView.findViewById(R.id.stat_amount);
        }
    }
}